﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LotteryVote.Logic.Web;
using LotteryVote.Entity;
using LotteryVote.Logic;
using System.Drawing;
using LotteryVote.Logic.Exceptions;
using LotteryVote.Utility;

namespace LotteryVote.Controls
{
    public partial class CompanyReport : BaseControl, IReportControl
    {
        public int UserId { get; set; }
        public DateTime FromDate { get; private set; }
        public DateTime ToDate { get; private set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            //    BindReport(DateTime.Today, DateTime.Today, UserId);
        }

        public void BindReport(DateTime fromDate, DateTime toDate, int userId)
        {
            this.FromDate = fromDate;
            this.ToDate = toDate;
            Execute(() =>
            {
                CheckRole(userId);
                SettleResultFacade SRFacade = new SettleResultFacade();
                rpt_winlostReport.DataSource = SRFacade.GetSettleResult(userId, fromDate, toDate);
                rpt_winlostReport.DataBind();
            });
        }

        void CheckRole(int userId)
        {
            UserFacade userFacade = new UserFacade();
            var family = userFacade.GetUserFamily(userId);
            if (family.Find(it => it.UserId == CurrentUser.UserId) == null)
                throw new NoPermissionException("查看输赢报表!");
        }

        int orderCount;
        decimal turnover, totalcomm, winlost, comm, totalWinlost, upperWinlost, upperComm, upperTotalWinlost;
        protected void rpt_winlostReport_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                SettleResult data = e.Item.DataItem as SettleResult;
                if (data != null)
                {
                    orderCount += data.OrderCount;
                    turnover += data.BetTurnover;
                    totalcomm += data.TotalCommission;
                    winlost += data.WinLost;
                    comm += data.Commission;
                    totalWinlost += data.TotalWinLost;
                    upperWinlost += data.UpperWinLost;
                    upperComm += data.UpperCommission;
                    upperTotalWinlost += data.UpperTotalWinLost;
                }
            }
            else if (e.Item.ItemType == ListItemType.Footer)
            {
                Label ltlordercount = e.Item.FindControl("ltl_orderCount") as Label;
                Label ltlturnover = e.Item.FindControl("ltl_totalTurnover") as Label;
                Label ltlcommisson = e.Item.FindControl("ltl_commission") as Label;
                Label ltlwinlost = e.Item.FindControl("ltl_winlost") as Label;
                Label ltlcomm = e.Item.FindControl("ltl_comm") as Label;
                Label ltltotalWinlost = e.Item.FindControl("ltl_totalWinlost") as Label;
                Label ltlupperWinlost = e.Item.FindControl("ltl_upperWinlost") as Label;
                Label ltlupperComm = e.Item.FindControl("ltl_upperComm") as Label;
                Label ltlupperTotalWinlost = e.Item.FindControl("ltl_upperTotalWinlost") as Label;

                if (ltlturnover != null && ltlcommisson != null && ltlcommisson != null && ltlwinlost != null)
                {
                    ltlordercount.ForeColor = orderCount > 0 ? Color.Black : Color.Red;
                    ltlordercount.Text = orderCount.ToString();

                    ltlturnover.ForeColor = turnover > 0 ? Color.Black : Color.Red;
                    ltlturnover.Text = turnover.ToString();

                    ltlcommisson.ForeColor = comm > 0 ? Color.Black : Color.Red;
                    ltlcommisson.Text = comm.ToString();

                    ltlwinlost.ForeColor = winlost > 0 ? Color.Black : Color.Red;
                    ltlwinlost.Text = winlost.ToString();

                    ltlcomm.ForeColor = comm > 0 ? Color.Black : Color.Red;
                    ltlcomm.Text = comm.ToString();

                    ltltotalWinlost.ForeColor = totalWinlost > 0 ? Color.Black : Color.Red;
                    ltltotalWinlost.Text = totalWinlost.ToString();

                    ltlupperWinlost.ForeColor = upperWinlost > 0 ? Color.Black : Color.Red;
                    ltlupperWinlost.Text = upperWinlost.ToString();

                    ltlupperComm.ForeColor = upperComm > 0 ? Color.Black : Color.Red;
                    ltlupperComm.Text = upperComm.ToString();

                    ltlupperTotalWinlost.ForeColor = upperTotalWinlost > 0 ? Color.Black : Color.Red;
                    ltlupperTotalWinlost.Text = upperTotalWinlost.ToString();
                }

            }
        }
    }
}